<?php
ob_start();
session_start();
require('twitteroauth.php');
$uri= explode("Service",$_SERVER['REQUEST_URI']);
$finaluri=$_SERVER['HTTP_HOST'].$uri[0];

$fileTW=fopen("../../twitter.txt","r") or exit ("do not open setting file!");
$leggi=@fread($fileTW,filesize("../../twitter.txt"));
$dynamic_urlTW=nl2br($leggi);

if($dynamic_urlTW != null){
         $temp = explode(";",$dynamic_urlTW);
         $id_TW = $temp[0];
         $secret_TW= $temp[1];
     }
	$consumer_key=$id_TW;
        $consumer_secret=$secret_TW;                            
        
        if(!isset($_GET['oauth_verifier'])){
            
            //application param consumer key consumer secret TWitter
            $oauth = new TwitterOAuth($consumer_key,$consumer_secret);
	
            $request = $oauth->getRequestToken();
            $requestToken = $request['oauth_token'];
            $requestTokenSecret = $request['oauth_token_secret'];
            
            
            $_SESSION['requestToken']=$requestToken;
            $_SESSION['requestTokenSecret']=$requestTokenSecret;

            // display Twitter generated registration URL
            $registerURL = $oauth->getAuthorizeURL($request);
     
            
            header('Location:'.$registerURL);

	
        }else{
        $oauth = new TwitterOAuth($consumer_key,$consumer_secret, $_SESSION['requestToken'], $_SESSION['requestTokenSecret']);  
        $o_verifier=$_GET['oauth_verifier'];  
        //print_r($oauth);
        //echo$access_token;
        
	$request = $oauth->getAccessToken($_GET['oauth_verifier']);
        
        //print_r($request);
        
	$accessToken = $request['oauth_token'];
	$accessTokenSecret = $request['oauth_token_secret'];
        
        
        
       
	$oauth = new TwitterOAuth($consumer_key,$consumer_secret, $accessToken, $accessTokenSecret);
	// Send an API request to verify credentials
	$credentials = $oauth->get("account/verify_credentials");
	$img=$credentials->profile_image_url_https;
        
	
        ?>
<script type="text/javascript"> 
    
Invia_TokenTW();

function Invia_TokenTW(){
     var urlimg = "<?php echo$img; ?>";
     var finaluri = "<?php echo$finaluri; ?>";
     location.href="http://"+finaluri+"Service/insert_token.php?access_token=<?php echo$accessToken.";".$accessTokenSecret; ?>&service=Twitter&urlimg="+urlimg; 
 }
</script>
 <?php           
        }
?>


        
        
	



